package org.lem

class ClassRoster {

  static hasMany = [studentLearningActivity: StudentLearningActivity, classDates: Date]

  ScheduledClass scheduledClass
  String month
  Integer year
  Double hoursPerClass = 1.5;
  List<Date> classDates

  public void computeScheduledClassDays() {
    int monthInt = 0

    if (month.equals("January")) {
      monthInt = Calendar.JANUARY;
    } else if (month.equals("February")) {
      monthInt = Calendar.FEBRUARY;
    } else if (month.equals("March")) {
      monthInt = Calendar.MARCH;
    } else if (month.equals("April")) {
      monthInt = Calendar.APRIL;
    } else if (month.equals("May")) {
      monthInt = Calendar.MAY;
    } else if (month.equals("June")) {
      monthInt = Calendar.JUNE;
    } else if (month.equals("July")) {
      monthInt = Calendar.JULY;
    } else if (month.equals("August")) {
      monthInt = Calendar.AUGUST;
    } else if (month.equals("September")) {
      monthInt = Calendar.SEPTEMBER;
    } else if (month.equals("October")) {
      monthInt = Calendar.OCTOBER;
    } else if (month.equals("November")) {
      monthInt = Calendar.NOVEMBER;
    } else if (month.equals("December")) {
      monthInt = Calendar.DECEMBER;
    }

    // Create a calendar object of the desired month
    Calendar cal = new GregorianCalendar(year, monthInt, 1);

    // Get the number of days in that month
    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    try {
      for (int day = 1; day <= days; day++) {
        Calendar calendarDay = new GregorianCalendar(year, monthInt, day);
        int dayOfWeek = calendarDay.get(Calendar.DAY_OF_WEEK);

        if (scheduledClass.getClassDays().getName().equals("MF")
                && (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.FRIDAY)) {
          addToClassDates(calendarDay.getTime())
        }
        else if (scheduledClass.getClassDays().getName().equals("MTh")
                && (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.THURSDAY)) {
          addToClassDates(calendarDay.getTime())
        }
        else if (scheduledClass.getClassDays().getName().equals("MW")
                && (dayOfWeek == Calendar.MONDAY || dayOfWeek == Calendar.WEDNESDAY)) {
          addToClassDates(calendarDay.getTime())
        }
        else if (scheduledClass.getClassDays().getName().equals("SAT")
                && dayOfWeek == Calendar.SATURDAY) {
          addToClassDates(calendarDay.getTime())
          hoursPerClass = 3.0
        }
        else if (scheduledClass.getClassDays().getName().equals("TTh")
                && (dayOfWeek == Calendar.TUESDAY || dayOfWeek == Calendar.THURSDAY)) {
          addToClassDates(calendarDay.getTime())
        }
      }
    } catch (Exception e) {
      e.printStackTrace()
    }
  }

  static constraints = {
    scheduledClass()
    month(inList: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
    year()
    hoursPerClass()
    classDates()
    studentLearningActivity()
  }

  String toString() {
    def toStringVal =
    "${month} ${year} ${scheduledClass.getLocation()} ${scheduledClass.getClassDays()} "+ "${scheduledClass.getClassTime()} ${scheduledClass.getSkillLevel()}"

    if(null != scheduledClass.getSession()) {
      toStringVal += " ${scheduledClass.getSession()}"
    }

    return toStringVal
  }
}
